const drag = {
  mounted(e){
    let	minL = 0,
			maxL = document.documentElement.clientWidth - e.offsetWidth,
			minT = 0,
			maxT = document.documentElement.clientHeight - e.offsetHeight;

		e.addEventListener('mousedown', down);

		// 鼠标按下
		function down(ev) {
      this.parentEvStartLeft = ev.target.parentNode.offsetLeft
      this.parentEvStartTop = ev.target.parentNode.offsetTop
			this.startX = ev.pageX;
			this.startY = ev.pageY;
			this._MOVE = move.bind(this);
			this._UP = up.bind(this);
			window.addEventListener('mousemove', this._MOVE);
			window.addEventListener('mouseup', this._UP);
		}

		// 鼠标滑动
		function move(ev) {
			let curLeft = ev.pageX - this.startX + this.parentEvStartLeft,
					curTop = ev.pageY - this.startY + this.parentEvStartTop;
			curLeft = curLeft < minL ? minL : (curLeft > maxL ? maxL : curLeft);
			curTop = curTop < minT ? minT : (curTop > maxT ? maxT : curTop);
			this.parentNode.style.left = curLeft + 'px';
			this.parentNode.style.top = curTop + 'px';
		}

		function up() {
			// this => e
			window.removeEventListener('mousemove', this._MOVE);
			window.removeEventListener('mouseup', this._UP);
		}
  }
}

export default drag